import re

import scrapy


class KuaikanSpider(scrapy.Spider):
    name = "kuaikan"
    # allowed_domains = ["www.kuaikanmanhua.com"]
    # start_urls = ["https://www.kuaikanmanhua.com"]

    async def start(self):
        urls = [f"https://www.kuaikanmanhua.com/tag/0?region=1&pays=0&state=0&sort=1&page={i}" for i in range(1, 209)]
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse, method="GET")
            break

    async def parse(self, response):
        items = response.xpath("//div[contains(@class, 'ItemSpecial fl')]")
        for item in items:
            detail_url = item.xpath('./a/@href').get()
            title = item.xpath("./a//span[@class='itemTitle']/text()").get()
            detail_url = f"https://www.kuaikanmanhua.com{detail_url}"
            yield scrapy.Request(url=detail_url, callback=self.parse_detail, method="GET")
            break

    async def parse_detail(self, response):
        # print(response.text)
        info = re.search(r'}}}}\((.*?)\)\);</script>', response.text).group()
        items = re.findall(r'(\d{6}),"(.*?)","https.*?",".*?","(\d{2}-\d{2}-\d{2})",', info)
        print(items)
        for item in items[1:]:
            pic_id = item[0]
            title = item[1]
            create_time = item[2]
            print(pic_id, title, create_time)
            pic_url = f"https://www.kuaikanmanhua.com/webs/comic-next/{pic_id}"
            yield scrapy.Request(url=pic_url, callback=self.parse_pic, method="GET")
            break

    def parse_pic(self, response):
        print(response.text)
        imgs = response.xpath("//img[@class='img']")
        print(imgs)
        img_urls = set()
        for img in imgs:
            img_url = img.xpath("/@src")
            print(img_url)
            break
        #     if img_url in img_urls:
        #         img_urls.add(img_url)
        # print(img_urls)

# https://p1.kkmh.com/image/c190853/190822/feiezBHjl.webp-t.w1280.jpg.h?sign=f614745f2c3a827a780ec2d41b4851e9&t=6911e71b
# https://p1.kkmh.com/image/c190853/190822/feiezBHjl.webp-t.w1280.jpg.h?sign=112a2f5cfc159a032fd3545dc5a10df2&t=6911e7e2